/**
 * Created by LinJe on 2015/9/18.
 * 全局ui：警告性的弹窗信息
 */
(function( $, window ) {

    window.globalLayer = window.globalLayer || {};

    /**
     * 调用实例
     * window.globalLayer.openWarning({'autoClose':true, 'time':2000, 'text':'服务器异常', 'callBack':function() {
            console.log('警告窗已关闭');
       }});
     * @param settings
     */
    window.globalLayer.openWarning = function(settings) {

        var warningArgs;

        //如果是第一次调用，加载弹窗所需的css文件 以及 html
        if ( !this.warningArgs ) {
            $("<link>").attr({ rel:"stylesheet", type:"text/css", href:"    /static/huihuo/skin/default/layer_warning.css"}).appendTo("head");
            $('body').append('\
                <div class="layer_warning">\
                    <div class="bg"></div>\
                    <div class="outer">\
                        <div class="middle">\
                            <div class="inner"></div>\
                        </div>\
                    </div>\
                </div>\
            ');
            warningArgs = this.warningArgs = {};
            warningArgs.$warningWrap = $('.layer_warning');
            warningArgs.$content = warningArgs.$warningWrap.find('.inner');
            warningArgs.timer = null;
        }

        warningArgs = this.warningArgs;
        $.extend(warningArgs, settings);

        warningArgs.$content.text( warningArgs.text || '' );
        warningArgs.$warningWrap.css({'opacity':'0', 'display':'block'}).stop().animate({ 'opacity': 1 }, 500, '', function() {
            if ( warningArgs.autoClose ) {
                clearTimeout(warningArgs.timer);
                warningArgs.timer = setTimeout( function() {
                    window.globalLayer.closeWarning(warningArgs.callBack);
                }, warningArgs.time );
            }
        });

    };


    window.globalLayer.closeWarning = function(callBack) {

        this.warningArgs.$warningWrap.stop().animate({ 'opacity': 0 }, 500, '', function() {
            $(this).hide();
            callBack && callBack();
        });

    };


})( jQuery, window );